<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/> 
    <link rel="stylesheet" href="../../css/swiper.min.css">
</head>
<body>
	<div class="swiper-container">
	    <div class="swiper-wrapper" id="swiper-wrapper">
	    <!--
	        <div class="swiper-slide"><img src="../../image/banner2.jpg" width="100%" ></div>
	        <div class="swiper-slide"><img src="../../image/banner2.jpg" width="100%" ></div>
	    -->
	    </div>
	    <div class="swiper-pagination"></div>
	</div>
	<div class="chanpin-div aui-margin-b-15">
		<div class="chanpin-tit aui-pull-left" id="goodsname"></div>
		<div class="aui-text-danger aui-font-size-18 aui-clearfix aui-pull-left" id="goodsprice"></div>
		<div class="chanpin-con aui-pull-left" id="goodsxiaoliang"></div>
		<p class="aui-clearfix"></p>
	</div>
	<div class="aui-content aui-margin-b-15" onclick="openArtinfo(4,'配送说明')">
	    <ul class="aui-list aui-list-in">
	        <li class="aui-list-item aui-list-item-middle ">
	            <div class="aui-list-item-inner aui-list-item-arrow">
	                <div class="aui-list-item-title tongcheng">
	                	<img src="../../image/duihao.png" width="15" class="aui-pull-left">
	                	<span class="aui-pull-left">快速送达</span>
	                	<img src="../../image/duihao.png" width="15" class="aui-pull-left">
	                	<span class="aui-pull-left">担保交易</span>
	                	<img src="../../image/duihao.png" width="15" class="aui-pull-left">
	                	<span class="aui-pull-left">同城配送</span>
	                </div>
	            </div>
	        </li>
        </ul>
	</div>
	<div class="aui-bar aui-bar-bt aui-bar-btn-full">
	    <div class="aui-bar-btn-itemm zhmx-active goodsshaixuanmenu" onclick="qiehuan(this,1);">产品详情</div>
	    <div class="aui-bar-btn-itemm goodsshaixuanmenu" onclick="qiehuan(this,2);">商品评论</div>
	</div>
	<div class="aui-grid goodsshaixuanbox" id="goodsinfo">
    	
    </div>
    <div class="aui-content goodsshaixuanbox none" id="goodscomment">
	    <ul class="aui-list aui-media-list" id="goodscommentlist">
	    
		
	    </ul>
	    <div id="wudongtai_box" class="none">
		 	<img src="../../image/wudongtai.png" id="wudongtai_img" />
			<p id="wudongtai_txt"> </p>
		 </div>	
		 <div class="aui-card-list-footer aui-text-center" id="cd_loading" onclick="loaddata();">点击查看更多</div>   
	</div>
    
    <footer class="aui-bar aui-bar-tab spinfo-foot">
		<div class="aui-col-xs-2 line2" style="border-right: 1px #ddd solid;">
			<i class="aui-iconfont aui-icon-star aui-font-size-18" id="goodsshoucang" onclick="shoucang();"></i>
		</div>
		<div class="aui-col-xs-2 line2" onclick="openMycart();" style="">
			<div class="aui-badge none" id="buy_num"></div>
			<i class="aui-iconfont aui-icon-cart aui-font-size-18"></i>
		</div>
		<div class="aui-col-xs-4 aui-bg-warning line2" onclick="AddToCar();">加入购物车</div>
		<div class="aui-col-xs-4 aui-bg-success line2" onclick="openBuygoods();">立即购买</div>
	</footer>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/zepto.js"></script>
<script type="text/javascript" src="../../script/swiper.min.js"></script>
<script>
var myParam,db_key = 'user',db_user,nowpage=1,loadend=false,isloading=false,buynumber=0;
var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
//初始化
apiready = function(){
    myParam = api.pageParam;
    getuser();//获取用户信息    
    loadinfo();//加载商品内容
    loaddata();//加载评论信息
    
    //监听登录
    api.addEventListener({
        name: 'login_self'
    }, function(ret, err) {
        getuser();//获取用户信息
    });
}
//获取用户储存在本地的数据方法
function getuser() {
    db_user = $api.getStorage(db_key);
}
//加载商品信息
function loadinfo() {
    var useParam={};
    	useParam.rtk = RSapi.myrtk;
    	useParam.mytoken = db_user.mytoken;
    	useParam.id = myParam.gid;
    api.ajax({
        url: RSapi.base+RSapi.shangchenginfo,
        method: 'post',
        cache: false,
        timeout: 30,
        dataType: 'json',
        data: { values: useParam },
    }, function(ret, err){
        if(err){
            //api.toast({msg: JSON.stringify(err)});
            api.alert({msg: err.msg});
        }else {
            if (ret.status>0) { 
                //成功了
                showslider(ret.xiangce);//显示轮播图           
                $api.html($api.dom("#goodsname"),ret.goodsinfo.title);//商品名称
                $api.html($api.dom("#goodsprice"),'￥'+ret.goodsinfo.price);//商品价格
                $api.html($api.dom("#goodsxiaoliang"),'已售：'+ret.goodsinfo.xiaoliang);//商品销量
                $api.html($api.dom("#goodsinfo"),ret.goodsinfo.content);//商品详情
                if(ret.shoucang == 0){
                	$api.removeCls($api.byId('goodsshoucang'),'F1C40F');
                }else{
                	$api.addCls($api.byId('goodsshoucang'),'F1C40F');
                }
                if(ret.cartnum > 0){
                	$api.removeCls($api.byId('buy_num'), 'none');
                	$api.html($api.byId('buy_num'),ret.cartnum);
                	buynumber = ret.cartnum;
                }
                api.parseTapmode();
            }else {
                //失败了
                api.toast({msg: ret.info});
            }
        }
    });
}
//显示轮播图列表
function showslider(list){
	var htmlstr='';
    var i;
    for (i in list) {
    	 htmlstr += '<div class="swiper-slide"><img src="'+list[i].thumb+'"/></div>';
    }
    $api.html($api.byId('swiper-wrapper'),htmlstr);
    slider();//swiper
}
//轮播图
function slider(){
	var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        paginationClickable: true,
        spaceBetween: 30,
        centeredSlides: true,
        autoplay: 2500,
        autoplayDisableOnInteraction: false
    });
}
//切换
function qiehuan(obj,type){
	var $shaixuanmenu_dom = $api.domAll(".goodsshaixuanmenu");
	var $shaixuanbox_dom = $api.domAll(".goodsshaixuanbox");
	for (var j = 0; j < $shaixuanmenu_dom.length; j++) {
		$api.removeCls($shaixuanmenu_dom[j], 'zhmx-active');
	}
	$api.addCls(obj,'zhmx-active');
	if(type == 1){
		$api.removeCls($api.byId('goodsinfo'), 'none');
		$api.addCls($api.byId('goodscomment'), 'none');
	}else if(type == 2){
		$api.addCls($api.byId('goodsinfo'), 'none');
		$api.removeCls($api.byId('goodscomment'), 'none');
	}
} 
//打开信息详情
function openArtinfo(id,title){
    api.openWin({
	        name: 'win_artinfo',
	        url: '../win/artinfo_win.html',
	        pageParam: {artid: id,pagetitle:title}
        });
}   
//加入收藏
function shoucang(){
	if (db_user.islogin) {
		var useParam = {
            rtk : RSapi.myrtk,
            mytoken : db_user.mytoken,
            sid : myParam.gid,        
            type : 'sc' 
        };
        api.ajax({
            url: RSapi.base+RSapi.favouriteCaozuo,
            method: 'post',
            cache: false,
            timeout: 30,
            dataType: 'json',
            data: { values: useParam },
        }, function(ret, err){
            if(err){
                //alert(JSON.stringify(err));
                api.alert({msg: JSON.stringify(err)});
            }else {
                if (ret.status == 1) {
                    //成功了
                    //更新收藏状态
                    if(ret.flag > 0){
                        $api.addCls($api.byId('goodsshoucang'),'F1C40F');
                        api.toast({msg:'加入收藏'});
                    }else{
                        $api.removeCls($api.byId('goodsshoucang'),'F1C40F');
                    	api.toast({msg:'取消收藏'});
                    }
                }else {
                    //失败了
                    api.toast({msg: ret.info});
                }
            }
        });
	}else {
        api.toast({ msg: '请先登录! ' });
        openLogin();
    } 
}
//加入购物车
function AddToCar(){
	if (db_user.islogin) {
		if(buynumber > 0){ api.toast({msg:'商品已加入购物车，请前往购物车结算！'}); return;}
		var useParam = {
	    	rtk : RSapi.myrtk,
	        mytoken : db_user.mytoken,
	        gid : myParam.gid,
	    };
	    api.ajax({
	        url: RSapi.base+RSapi.Tocar,
	        method: 'post',
	        cache: false,
	        timeout: 30,
	        dataType: 'json',
	        data: { values: useParam },
	    }, function(ret, err){
	        if(err){
	            //alert(JSON.stringify(err));
	            api.alert({msg: JSON.stringify(err)});
	        }else {
	            if (ret.status == 1) {
	            	//发送购物车更改事件
	                api.sendEvent({
	                    name: 'goods_tocar',
	                    extra: {}
	                });
	                //成功了
	                $api.removeCls($api.byId('buy_num'), 'none');
	                $api.html($api.byId('buy_num'),1);//当前页面购物车数量显示为1
	                api.toast({msg: '加入购物车成功！'});
	            }else {
	                //失败了
	                api.toast({msg: ret.info});
	            }
	        }
	    });
	    return false;
    }else {
        api.alert({ msg: '请先登录! ' });
        openLogin();
    }
}
//登录
function openLogin(param) {
	api.openWin({
		name : "win_login",
		url : '../win/login_win.html',
		pageParam: {inself:true,param:param,winname:api.winName}
	});
}
//打开我的购物车
function openMycart(){
	api.openWin({
		name : "win_cartleft",
		url : '../win/cartleft_win.html',
	});
}
//打开直接购买页面
function openBuygoods(){
	if (db_user.islogin) {
		api.openWin({
	        name: 'win_buygoods',
	        url: '../win/buygoods_win.html',
	        pageParam: {gid: myParam.gid}
	    });
	}else {
        api.toast({ msg: '请先登录! ' });
        openLogin();
    }   
}
//加载评论内容
function loaddata() {
	if (loadend) {return;}//如果已经加载完成，停止执行
    if (isloading) {return;}//如果正在加载，禁止重复加载
    else isloading = true;//设置当前状态为正在加载中
    api.showProgress();//显示加载进度框
//  $api.removeCls($api.byId("cd_loading"),'none');//显示加载中
	
	//加载评论所需参数
	var useParam={
		'page':nowpage,
		'rtk':RSapi.myrtk,
		'mytoken':db_user.mytoken,
		'gid':myParam.gid
	};
	//请求评论数据
	api.ajax({
		url: RSapi.base+RSapi.goodsCommentList,
		method: 'post',
		cache: false,
		timeout: 30,
		dataType: 'json',
		data: { values: useParam },
	}, function(ret, err){
		if(err){
			api.alert({msg: err.msg});//api.toast({msg: JSON.stringify(err)});
		}else {
			if (ret.status==1) { 
				//请求数据成功了
				$api.addCls($api.byId('wudongtai_box'), 'none');//隐藏无动态框	
				nowpage++;//页数加一
				$api.append($api.byId('goodscommentlist'),ret.info);//展示请求到的评论内容					
				api.parseTapmode();
				eval('status_'+status+'=true;');
			}else if(ret.status==2){
				//当前为第一页，并且没有请求到内容
				$api.html($api.byId('goodscommentlist'),'');
				loadend = true;//设置为加载完成，下拉不再进行数据请求
				$api.removeCls($api.byId('wudongtai_box'), 'none');//显示无动态框
				$api.html($api.byId('wudongtai_txt'), ret.info);//显示提示内容
				$api.addCls($api.byId("cd_loading"),'none');//隐藏加载中
			}else if(ret.status==3){
				//成功了, 但是已经加载完毕
                loadend = true;//设置为加载完成，下拉不再进行数据请求
                $api.addCls($api.byId("cd_loading"),'none');//隐藏加载中
                api.toast({msg:ret.info});
			}else {
				//失败了
				api.toast({msg: ret.info});
			}
		}
		isloading = false;//加载结束
	});
	api.hideProgress();
}
</script>
</html>